var ec_right1 = echarts.init(document.getElementById("r1"), 'my');

var option_right1 = {
	tooltip: {
		trigger: 'axis',
		axisPointer: {
			type: 'shadow'
		}
	},
	dataZoom: [
		{
			type: "slider",
			show: false,
			startValue: 0,
			endValue: 4,
			yAxisIndex: [0],
		},
	],
	legend: {
		textStyle: {
			fontSize: 10,
			color: '#ffffff'
		},
		data: ['涨', '未涨'],
	},
	grid: {
		left: '3%',
		right: '4%',
		bottom: '3%',
		containLabel: true
	},
	xAxis: [
		{
			type: 'value',
			axisLabel: {
				color: "rgba(255,255,255,.6)",
				fontSize: "12"
			},
		}
	],
	yAxis: [
		{
			type: 'category',
			axisLabel: {
				color: "rgba(255,255,255,.6)",
				fontSize: "12"
			},
			axisTick: {
				show: false
			},
			data: []
		}
	],
	series: [
		{
			name: '涨',
			type: 'bar',
			stack: 'Total',
			color: '#93b5cf',
			label: {
				show: true
			},
			emphasis: {
				focus: 'series'
			},
			data: []
		},
		{
			name: '未涨',
			type: 'bar',
			stack: 'Total',
			color: '#1177b0',
			label: {
				show: true,
				position: 'left'
			},
			emphasis: {
				focus: 'series'
			},
			data: []
		}
	]
};

function get_r1_data() {
	$.ajax({
		url: "/r1",
		success: function (data) {
			option_right1.yAxis[0].data = data.n;
			option_right1.series[0].data = data.c1;
			option_right1.series[1].data = data.c2;
			if (option_right1.dataZoom[0].endValue == data.length - 1) {
				option_right1.dataZoom[0].endValue = 4;
				option_right1.dataZoom[0].startValue = 0;
			} else {
				option_right1.dataZoom[0].endValue = option_right1.dataZoom[0].endValue + 1;
				option_right1.dataZoom[0].startValue = option_right1.dataZoom[0].startValue + 1;
			}
			ec_right1.setOption(option_right1);
			if (option_right1.dataZoom[0].endValue == 4) {
				// 如果轮播完所有数据，延迟一段时间后重新开始
				setTimeout(function () {
					option_right1.dataZoom[0].endValue = 0;
					option_right1.dataZoom[0].startValue = 0;
					ec_right1.setOption(option_right1);
				}, 1000);
			}
		},
		error: function (xhr, type, errorThrown) {
		}
	});
}

get_r1_data();
setInterval(get_r1_data, 2000);

window.addEventListener("resize", function () {
	ec_right1.resize();
});
